package com.dev.dream.exochatlockscreen.slidemenu;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.TypedArray;
import android.net.ParseException;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.dev.dream.exochatlockscreen.R;
import com.dev.dream.libs.adapter.AppAdsAdapter;
import com.dev.dream.libs.adapter.model.AppAdsItem;
import com.dev.dream.libs.checknetwork.CheckNetword;
import com.dev.dream.libs.listviewanimation.simple.SwingBottomInAnimationAdapter;
import com.startapp.android.publish.StartAppSDK;

public class HomeFragment extends Fragment {
	private static final int INITIAL_DELAY_MILLIS = 300;

	ListView lv_app_ads;
	private String[] name_app_ads;
	private String[] url_app_ads, rate_app_ads;
	private TypedArray icon_app_ads;

	private ArrayList<AppAdsItem> appAdsItems;

	AppAdsAdapter adapter;
	LinearLayout llError;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		StartAppSDK.init(getActivity(), "111076217 ", "207788425", true);

		// load slide menu items

		View rootView = inflater.inflate(R.layout.fragment_home, container,
				false);
		
		lv_app_ads = (ListView) rootView.findViewById(R.id.lv_app_ads);
		llError = (LinearLayout) rootView.findViewById(R.id.llError);

		appAdsItems = new ArrayList<AppAdsItem>();

		if (CheckNetword.checkInternetConnection(getActivity())) {

			llError.setVisibility(View.GONE);
			
			new JSONAsyncTask()
					.execute("http://devdreamvn.somee.com/kpopapps.json");

			adapter = new AppAdsAdapter(getActivity(), appAdsItems);

			SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(
					adapter);
			swingBottomInAnimationAdapter.setAbsListView(lv_app_ads);
			lv_app_ads.setAdapter(swingBottomInAnimationAdapter);

			// lv_app_ads.setAdapter(adapter);

			lv_app_ads.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub

					openUrl(appAdsItems.get(arg2).getUrl());
				}
			});
		}

		return rootView;
	}

	private void openUrl(String url) {
		try {
			startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
		} catch (Exception e) {

		}
	}
	
	

	class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

		ProgressDialog dialog;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new ProgressDialog(getActivity());
			dialog.setMessage("Loading, please wait");
			// dialog.setTitle("Connecting server");
			dialog.show();
			dialog.setCancelable(false);

		}

		@Override
		protected Boolean doInBackground(String... urls) {
			try {

				// ------------------>>
				HttpGet httppost = new HttpGet(urls[0]);
				HttpClient httpclient = new DefaultHttpClient();
				HttpResponse response = httpclient.execute(httppost);

				// StatusLine stat = response.getStatusLine();
				int status = response.getStatusLine().getStatusCode();

				if (status == 200) {
					HttpEntity entity = response.getEntity();
					String data = EntityUtils.toString(entity);

					JSONObject jsono = new JSONObject(data);
					JSONArray jarray = jsono.getJSONArray("apps");

					for (int i = 0; i < jarray.length(); i++) {
						JSONObject object = jarray.getJSONObject(i);

						AppAdsItem appAdsItem = new AppAdsItem();

						appAdsItem.setId(Integer.parseInt(object
								.getString("id")));
						appAdsItem.setName(object.getString("name"));
						appAdsItem.setUrl(object.getString("url"));
						appAdsItem.setRate(object.getString("rate"));
						appAdsItem.setUrlIcon(object.getString("urlicon"));

						appAdsItems.add(appAdsItem);

					}
					return true;
				}

				// ------------------>>

			} catch (ParseException e1) {
				e1.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return false;
		}

		protected void onPostExecute(Boolean result) {

			dialog.cancel();
			adapter.notifyDataSetChanged();
			if (result == false) {
				llError.setVisibility(View.VISIBLE);

			}

		}
	}

}
